<?
namespace Database;

class Mysql extends \Database{
    private $link = false;
    private $db = false;
    
    public function __construct($params) {
        $this->px = !empty($params['prefix']) ? $params['prefix'] : "";
        $link = mysql_connect($params['server'], $params['username'], $params['password']);
        if($link){
            $db = mysql_select_db($params['database']);
            if($db){
                $this->db = $params['database'];
                $this->link = $link;
            }else{
                \Logger::error(mysql_error($link));
            }
        }else{
            \Logger::error(mysql_error());
        }
        return $this;
    }
    
    function getTables(){
        $result = $this->native("SHOW TABLES FROM ".$this->db)->exec();
        $tables = array();
        while($table = $result->fetch()){
            $tables[] = array_shift($table);
        }
        return $tables;
    }
    
    function exec() {
        $sql = $this->GetSql();        
        $result = mysql_query($sql, $this->link);
        $this->data = array();
        $this->action = false;
        if(!$result){
            \Logger::error(mysql_error($this->link));
        }
        IncludeClass("\\Database\\Mysql\\ResultObject", __DIR__);
        return new Mysql\ResultObject($result, $sql);
    }

}
